package com.htht.job.admin.controller.product;

import com.alibaba.fastjson.JSON;
import com.htht.job.core.api.DubboService;
import com.htht.job.core.api.treenode.TreeNodeService;
import com.htht.job.core.biz.model.ReturnT;
import com.htht.job.executor.model.product.Product;
import com.htht.job.executor.model.tree.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/product")
public class ProductController {
	@Autowired
	private TreeNodeService treeNodeService;

	@Autowired
	private DubboService dubboService;

	@RequestMapping
	public String index() {
		return "/product/product.index";
	}
	
	@RequestMapping("/getTreeNode")
	@ResponseBody
	public String getTreeNode(){
		List<TreeNode> treeNodes =treeNodeService.getTreeNodeById();
		return JSON.toJSONString(treeNodes);
	}

	@RequestMapping("/save")
	@ResponseBody
	public ReturnT<String> save(TreeNode treeNode){
		TreeNode t=treeNodeService.saveTreeNode(treeNode);
		if(null!=t.getId()){
			return ReturnT.SUCCESS;
		}else{
			return ReturnT.FAIL;
		}
	}
	@RequestMapping("/deleteTreeNode")
	@ResponseBody
	public ReturnT<String> deleteTreeNode(String id){
		return treeNodeService.deleteTreeNode(id);
	}

	@RequestMapping("/pageList")
	@ResponseBody
	public Map<String, Object> pageList(@RequestParam(required = false, defaultValue = "0") int start,  
			@RequestParam(required = false, defaultValue = "10") int length,Product product) {
		//parameterModel.setModelName("1");
		if(start!=0){
			start=start/length;
		}
		return dubboService.pageListProduct(start, length, product);
	}
	@RequestMapping("/saveProduct")
	@ResponseBody
	public ReturnT<String> saveProduct(Product product){
		Product p=dubboService.saveProduct(product);
		if(null!=p.getId()){
			return ReturnT.SUCCESS;
		}else{
			return ReturnT.FAIL;
		}
	}
	@RequestMapping("/deleteProduct")
	@ResponseBody
	public  ReturnT<String> deleteProduct(String id){
		return dubboService.deleteProduct(id);
	}

	@RequestMapping("/select")
	@ResponseBody
	public String select(){
		List<Map<String, Integer>> list=new ArrayList<>();
		for(int i=0;i<50;i++){
			Map<String, Integer> map=new HashMap<String, Integer>();
			map.put("id",i);
			map.put("text",i);

			list.add(map);
	}
		return JSON.toJSONString(list);
	}

}
